#include "Menu.h"

using namespace mega;
using namespace sf;

Menu::Menu()
	: GameView()
	, m_bgColor(Color::Black)
	, m_btnColor(Color::Black)
	, m_btnBorderColor(Color(Uint8(64), Uint8(0), Uint8(0)))
	, m_btnTextColor(Color(Uint8(128), Uint8(0), Uint8(0)))
	, m_btnTextSize(20)
	, m_shape()
{}


Menu::~Menu()
{
	for (auto iter = std::begin(m_items); iter != std::end(m_items); ++iter)
	{
		delete *iter;
		*iter = nullptr;
	}
}

// TODO: provide clear initialization logic
void Menu::initItems()
{
	m_font.loadFromFile("fonts/arial.ttf");

	for (auto iter = std::begin(m_items); iter != std::end(m_items); ++iter)
	{
		float menuItemHeight     = (*iter)->m_shape.getSize().y;
		float menuItemTextOffset = 0.5f * ((*iter)->m_shape.getSize().y - m_btnTextSize);
		
		// setup buttons shape properties
		(*iter)->m_shape.setFillColor(m_btnColor);
		(*iter)->m_shape.setOutlineColor(m_btnBorderColor);
		(*iter)->m_shape.setOutlineThickness(menuItemHeight / 10);
	
		// setup buttons text properties
		(*iter)->m_text.setFont(m_font);
		(*iter)->m_text.setCharacterSize(m_btnTextSize);
		//m_text.setString(_text);
		(*iter)->m_text.setColor(m_btnTextColor);
		(*iter)->m_text.setPosition(m_shape.getPosition() + Position(menuItemTextOffset, 10.f));
	}
}


// TODO: provide clear expanding logic
void Menu::expand()
{
	float shapeWidth  = m_shape.getSize().x;
	float shapeHeight = m_shape.getSize().y;
	
	Vector2f offset(50.f, 20.f);

	size_t counter = 0;
	for (auto iter = std::begin(m_items); iter != std::end(m_items); ++iter)
	{
		(*iter)->move(offset + Vector2f(0., 70.f * counter++));
	}
}


void Menu::render(mega::RenderTarget & _target)
{
	_target.draw(m_shape);

	for (auto iter = std::begin(m_items); iter != std::end(m_items); ++iter)
		(*iter)->render(_target);
}


void Menu::move(const Position & _offset)
{
	GameObject::move(_offset);

	m_shape.move(_offset);

	for (auto iter = std::begin(m_items); iter != std::end(m_items); ++iter)
		(*iter)->move(_offset);
}